import java.util.Random;
import java.util.Scanner;

public class Lab_01 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Random random = new Random();
        int correctCount = 0;
        int totalExercises = 50;
        
        System.out.println("===== 100以内加减法口算练习 =====");
        System.out.println("请在每道题目后输入答案，按Enter键确认");
        System.out.println("==================================");
        
        for (int i = 1; i <= totalExercises; i++) {
            // 生成两个100以内的随机数
            int num1 = random.nextInt(100);
            int num2 = random.nextInt(100);
            
            // 随机选择加法或减法
            boolean isAddition = random.nextBoolean();
            int correctAnswer;
            String operator;
            
            if (isAddition) {
                // 加法：确保结果不超过100
                while (num1 + num2 > 100) {
                    num1 = random.nextInt(100);
                    num2 = random.nextInt(100);
                }
                correctAnswer = num1 + num2;
                operator = "+";
            } else {
                // 减法：确保结果非负
                if (num1 < num2) {
                    // 交换两个数，确保被减数大于等于减数
                    int temp = num1;
                    num1 = num2;
                    num2 = temp;
                }
                correctAnswer = num1 - num2;
                operator = "-";
            }
            
            // 显示题目并获取用户答案
            System.out.print("第" + i + "题: " + num1 + " " + operator + " " + num2 + " = ");
            int userAnswer = scanner.nextInt();
            
            // 检查答案是否正确
            if (userAnswer == correctAnswer) {
                System.out.println("✓ 正确！");
                correctCount++;
            } else {
                System.out.println("✗ 错误！正确答案是: " + correctAnswer);
            }
        }
        
        // 显示最终成绩
        System.out.println("==================================");
        System.out.println("练习完成！");
        System.out.println("总题目数: " + totalExercises);
        System.out.println("正确题数: " + correctCount);
        System.out.println("正确率: " + String.format("%.1f", (double)correctCount / totalExercises * 100) + "%");
        
        scanner.close();
    }
}